/**
 * editor_plugin_src.js
 *
 * Copyright 2009, Moxiecode Systems AB
 * Released under LGPL License.
 *
 * License: http://tinymce.moxiecode.com/license
 * Contributing: http://tinymce.moxiecode.com/contributing
 */

(function () {
    var each = tinymce.each;

    // Load plugin specific language pack
    tinymce.PluginManager.requireLangPack('frediumplugins');

    tinymce.create('tinymce.plugins.FrediumPlugins', {
        init: function (ed, url) {
            var t = this;
            t.editor = ed;
            t.url = url;
            ed.addCommand('mceFrediumPlugins', function () {
                ed.windowManager.open({
                    file: url + '/frediumplugins.aspx',
                    width: 500 + parseInt(ed.getLang('frediumplugins.delta_width', 0)),
                    height: 500 + parseInt(ed.getLang('frediumplugins.delta_height', 0)),
                    inline: 1
                }, {
                    plugin_url: url, // Plugin absolute URL
                    some_custom_arg: 'custom arg' // Custom argument
                });
            });

            // Register example button
            ed.addButton('frediumplugins', {
                title: 'frediumplugins.desc',
                cmd: 'mceFrediumPlugins',
                image: url + '/img/plugin.png'
            });

            function isFrediumPluginElm(n) {
                return /^(mceItemFrediumPlugin)$/.test(n.className);
            };

            // Add a node change handler, selects the button in the UI when a image is selected
            ed.onNodeChange.add(function (ed, cm, n) {
                cm.setActive('frediumplugins', n.nodeName == 'IMG' && isFrediumPluginElm(n));
            });

            ed.onInit.add(function () {
                var lo = {
                    mceItemFrediumPlugin: 'plugin'
                };

                if (ed.settings.content_css !== false)
                    ed.dom.loadCSS(url + "/css/content.css");
            });

            ed.onBeforeSetContent.add(t._pluginsToImages, t);

            ed.onPreProcess.add(function (ed, o) {
                var dom = ed.dom;
                if (o.get) {
                    each(dom.select('IMG', o.node), function (n) {
                        if (n.className == "mceItemFrediumPlugin")
                            dom.replace(t._buildPlugin(n), n);
                    });
                }
            });
        },

        getInfo: function () {
            return {
                longname: 'Example plugin',
                author: 'Some author',
                authorurl: 'http://tinymce.moxiecode.com',
                infourl: 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/example',
                version: "1.0"
            };
        },

        _pluginsToImages: function (ed, o) {
            var t = this, h = o.content;
            h = h.replace(/<frediumplugin\s*([^>]*)>&nbsp;<\/frediumplugin>/gi, function (a, b) {
                //b now holds the parameters on the form [parameter1="value1" parameter2="value2"]
                //convert it to the form [parameter1:'value1',parameter2:'value2']
                var parameters = b.match(/[^=^\s]*="[^"]*"/gi);
                var json = "";
                for (var i = 0; i < parameters.length; i++) {
                    json += parameters[i].substr(0, parameters[i].indexOf("=")) + ":'";
                    json += parameters[i].substr(parameters[i].indexOf("=") + 2);
                    json = json.substr(0, json.length - 1);
                    json += "',";
                }
                json = json.substr(0, json.length - 1); //remove last comma
                return '<img class="mceItemFrediumPlugin" title="' + ed.dom.encode(json) + '" src="' + t.url + '/img/trans.gif" width="100" height="100" />'
            });
            o.content = h;
        },

        _buildPlugin: function (n) {
            var ob, ed = this.editor, dom = ed.dom;
            ob = dom.create('frediumplugin', this._parse(n.title), '&nbsp;');
            return ob;
        },

        _parse: function (s) {
            return tinymce.util.JSON.parse('{' + s + '}');
        },

        _serialize: function (o) {
            return tinymce.util.JSON.serialize(o).replace(/[{}]/g, '');
        }
    });

    // Register plugin
    tinymce.PluginManager.add('frediumplugins', tinymce.plugins.FrediumPlugins);
})();